// https://leetcode.cn/problems/minimum-size-subarray-sum/
// Created by ade on 2022/9/2.
//

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
    // 思路：双指针left right, 不断递归该区间内和是否 >= target
    int minSubArrayLen(int target, vector<int> &nums) {
        int diff = INT_MAX;
        int l = 0, r = 1;
        int sum = nums[0];
        if (sum >= target) return 1;
        while (r < nums.size()) {
            sum += nums[r];
            while (l <= r && sum >= target) {
                sum -= nums[l];
                diff = min(diff, r - l + 1);
                l++;
                if (diff == 1) return 1;
            }
            r++;
        }
        return diff == INT_MAX ? 0 : diff;
    }
};

int main() {
    Solution so;
    vector<int> nums = {2, 3, 1, 2, 4, 3, 7};
    cout << so.minSubArrayLen(7, nums) << endl;
    return 0;
}
